package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.Food;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.FoodMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = FoodService.class)
@Transactional
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodMapper foodDao;
    //分页查询
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Food> foodPage= foodDao.findPage(queryString);
        return new PageResult(foodPage.getTotal(),foodPage.getResult());
    }

    //新增
    @Override
    public void addFood(Food food)   {
            foodDao.addfood(food);
    }

    @Override
    public Result findFoodByid(Integer id) {
        try {
            Food food=  foodDao.findFoodByid(id);
            return new Result(true,"开始编辑",food);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"启动编辑失败");
        }

    }

    @Override
    public void updateFood(Food food) {
     Long along=foodDao.update(food);
        if (along==0){
            throw new RuntimeException("编辑失败");
        }
    }

    //删除食物
    @Override
    public Result delete(Integer foodId) {
            try {
                foodDao.deleteFood_id4food_checkgroup(foodId);
                foodDao.deleteFoodById(foodId);
                return  new Result(true,"删除食物成功");
            } catch (Exception e) {
                e.printStackTrace();
            }
        return new Result(false,"删除食物失败!可能出现未知错误,请刷新页面后重试");
    }



}
